package com.beidu.lottery.call.service.portal.filter;

import com.beidu.lottery.call.constant.ActivityTypeEnum;
import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.exception.ServiceException;
import com.beidu.lottery.call.service.portal.chain.AbstractChainHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 抽奖活动规则校验校验：活动起止时间、活动上下架状态
 **/
@Component
@Slf4j
public class LotteryRuleValidate implements AbstractChainHandler<PortalActivityRule> {

    @Override
    public void handler(PortalActivityRule rule) {
        log.info("LotteryRuleValidate start");
        LocalDateTime now = LocalDateTime.now();
        // 未开始
        if (now.isBefore(rule.getActivityBeginTime())) {
            throw new ServiceException("活动未开始");
        }
        // 已结束
        if (now.isAfter(rule.getActivityEndTime())) {
            throw new ServiceException("活动已经结束");
        }
    }

    @Override
    public String mark() {
        return ActivityTypeEnum.LOTTERY.getType();
    }

    @Override
    public int getOrder() {
        return 10;
    }
}